import Tween from "../../core/anim/Tween";
import game from "../../core/game";
import { UIBase } from "../../core/gui/UIBase"
import UIManage from "../../core/gui/UIManage";
import UIRes from "../../core/gui/UIRes";
import { Logger } from "../../core/log/logger";
import LoadUtils from "../../core/utils/LoadUtils";
import { ResEnum } from "../../module/common/ResEnum";
import UI_logging from "../../moduleui/login/UI_logging";
import MainUI from "../main/MainUI";

export default class Loading extends UIBase<UI_logging> {

    createView(): UI_logging {

        return UI_logging.createInstance();
    }

    initialize(): void {
        let view = this.view;
        view.setSize(game.width, game.height);
    }

    onOpen(): void {
        let view = this.view;

        // view.m_btn_group.visible = false;
        view.m_pros.visible = true;

        let progress = LoadUtils.create(["pkg", "assets", "data"], [20, 80, 60]);
        progress.valueHandler = Laya.Handler.create(this, this.onValueChange, null, false)

        UIRes.I.loadPackage([ResEnum.common], Laya.Handler.create(this, () => {
            progress.setValue("pkg", 1)
        }), Laya.Handler.create(this, v => {
            progress.setValue("pkg", v)
        }))

        game.data.loadData((v) => {
            progress.setValue("data", v)
        });

        // progress.setValue("assets", 1);
        //模拟加载，让进度条走慢一点
        let v = 0;
        let assist = setInterval(() => {
            v += 3;
            progress.setValue("assets", v / 100);

            if (v >= 100) {

                clearInterval(assist);
            }

        }, 30);
    }

    onValueChange(v: number) {
        let currV = 100 * v;
        if (Math.abs(this.view.m_pros.value - currV) > 0) {
            Tween.once(this.view.m_pros).to({ value: currV }, 100);
        }
        if (v >= 1) {
            console.log("加载成功");
            this.close();

            game.openui(MainUI);
        }
    }
}